const app = getApp()
var address = require('../../../components/gk-city/address.json')
Page({
    data: {
      provincedata: [{ text: "请选择", value: "" }], //选择省市区默认值
      cityText: [], //省市区信息
      cityValue: [],
      isChecked: false, //控制默认地址开关
      isTips: true, //控制头部提示开关
      id: "", //地址id
      data: {}, //修改地址数据
      isEdit: false, //是否编辑
      isFromOrder: false, // 是否是从订单页面跳转过来的修改地址
      orderId: "",
    },
    onLoad(e) {
		this.isFromOrder = e.orderId != undefined // 判断是否来自订单模块中的修改地址
		this.orderId = e.orderId
		const that = this
		if (e && e.id) {
			let addressList = my.getStorageSync({key: 'addressList'}).data
			addressList.map((item, index)=> {
				if (item.id == e.id) {
					let res = item
					res.province = this.getCode(address, res.provinceStr, res.cityStr, res.areaStr).province * 1
					res.city = this.getCode(address, res.provinceStr, res.cityStr, res.areaStr).city * 1
					res.area = this.getCode(address, res.provinceStr, res.cityStr, res.areaStr).area * 1
					console.log(res)
					// return
					this.setData({
						data: res,
						cityText: [
						  res.provinceStr,
						  res.cityStr,
						  res.areaStr,
						],
						cityValue: [res.province, res.city, res.area],
						provincedata: [
						  { text: res.provinceStr, value: res.province },
						  { text: res.cityStr, value: res.city },
						  { text: res.areaStr, value: res.area },
						],
						isChecked: res.isDefault,
						isEdit: true
					 })
				}
			})
// 			this.setData({
// 				data: {
//   "id": 177756,
//   "createTime": "2022-10-09 17:20:44",
//   "updateTime": "2022-10-09 17:20:44",
//   "deleteTime": null,
//   "uid": "cfc882bfd505214dd1dcf9a56e73bfb6816b9fe2y4k60hvcl44e",
//   "province": 140000,
//   "provinceStr": "山西省",
//   "city": 140400,
//   "cityStr": "长治市",
//   "area": 140423,
//   "areaStr": "襄垣县",
//   "street": "襄垣县城",
//   "zcode": null,
//   "telephone": "13613563724",
//   "realname": "李雅芝",
//   "isDefault": true
// },
// 				cityText: [
// 				  "山西省",
// 				  "长治市",
// 				  "襄垣县",
// 				],
// 				cityValue: [140000, 140400, 140423],
// 				provincedata: [
// 				  { text: "山西省", value: 140000 },
// 				  { text: "长治市", value: 140400 },
// 				  { text: "襄垣县", value: 140423 },
// 				],
// 				isChecked: true,
// 				isEdit: true
// 			})
		  // this.$request(
		  //   "/hzsx/userAddress/getUserAddressById",
		  //   { id: e.id },
		  //   "get",
		  //   true
		  // ).then((res) => {
			 //  this.setData({
				// data: res.data,
				// cityText: [
				//   res.data.provinceStr,
				//   res.data.cityStr,
				//   res.data.areaStr,
				// ],
				// cityValue: [res.data.province, res.data.city, res.data.area],
				// provincedata: [
				//   { text: res.data.provinceStr, value: res.data.province },
				//   { text: res.data.cityStr, value: res.data.city },
				//   { text: res.data.areaStr, value: res.data.area },
				// ],
				// isChecked: res.data.isDefault,
				// isEdit: true
			 //  })
		    // this.data = res.data
		    // this.cityText = [
		    //   res.data.provinceStr,
		    //   res.data.cityStr,
		    //   res.data.areaStr,
		    // ]
		    // this.cityValue = [res.data.province, res.data.city, res.data.area]
		    // that.provincedata = [
		    //   { text: res.data.provinceStr, value: res.data.province },
		    //   { text: res.data.cityStr, value: res.data.city },
		    //   { text: res.data.areaStr, value: res.data.area },
		    // ]
		    // this.isChecked = res.data.isDefault
		    // this.isEdit = true
		  // })
		}
    },
	handleRef (ref) {
		// console.log(ref)
		this.ref = ref
	},
	openMultiLevelSelect () {
		// console.log(this.deepCode(address, "长治市"))
		this.ref.show()
	},
	getCode (list, pro, cit, are) {
		var province, city, area
		list.map((item, index)=> {
			if (item.text ==  pro) {
				province = item.value
				if (item.children) {
					item.children.map((item1, index1)=> {
						if (item1.text ==  cit) {
							city = item1.value
							if (item1.children) {
								item1.children.map((item2, index2)=> {
									if (item2.text ==  are) {
										area = item2.value
									}
								})
							}
						}
					})
				}
			}
		})
		return {province, city, area}
	},
	// deepCode (list, val) {
	// 	var res = ''
	// 	function getItem (list, val) {
	// 		list.map((item, index)=> {
	// 			// console.log(item)
	// 			if (item.text == val) {
	// 				// console.log(item)
	// 				res = item.value
	// 			} else {
	// 				if (item.children) {
	// 					getItem(item.children, val)
	// 				}
	// 			}
	// 		})
	// 	}
	// 	getItem(list,val)
	// 	return res
	// },
	//获取省市区信息
	getpickerParentValue(val) {
	  let _city = val
	  // this.cityText = _city.map((item) => item.text)
	  // this.cityValue = _city.map((item) => item.value)
	  // this.provincedata = val
	  this.setData({
		  cityText: _city.map((item) => item.text),
		  cityValue:  _city.map((item) => item.value),
		  provincedata: val
	  })
	},
	
	//新增
	add(street, telephone, realname) {
	  const url = this.isFromOrder ? "hzsx/api/order/userOrderAddressModify" : "hzsx/userAddress/addUserAddress"
	  const postData = {
	    province: this.data.cityValue[0],
	    city: this.data.cityValue[1],
	    area: this.data.cityValue[2],
	    street,
	    telephone,
		provinceStr: this.data.cityText[0],
		cityStr: this.data.cityText[1],
		areaStr: this.data.cityText[2],
	  }
	
	  if (this.data.isFromOrder) { // 来自订单模块修改地址时
	    postData.orderId = this.data.orderId
	    postData.realName = realname
	  } else {
	    postData.isDefault = this.data.isChecked
	    postData.realname = realname
	    // postData.uid = this.$getUid()
	  }
	
	let list = my.getStorageSync({key: 'addressList'}).data != null ? my.getStorageSync({key: 'addressList'}).data : []
	let ran = (Math.random() + new Date().getTime()).toString(32).slice(0,8)
	postData.id = ran
	// console.log(ran)
	// console.log(list)
	list.push(postData)
	my.setStorageSync({
	  key: 'addressList',
	  data: list,
	});
	setTimeout(()=> {
		my.navigateBack({
			delta: 1,
		})
	}, 50)
	  // this.$request(
	  //   url,
	  //   postData,
	  //   "post"
	  // ).then(() => {
	  //   if (this.data.isFromOrder) { // 来自订单模块时，修改完地址时通知组件更新
	  //     notifyTopPageRefresh.apply(this) // 通知订单页面、订单详情页面进行刷新操作
	  //   }
	  //   my.navigateBack({
	  //     delta: 1,
	  //   })
	  // })
	},
	
	//修改
	modify(street, telephone, realname) {
		const id = this.data.data.id
		let addressList = my.getStorageSync({key: 'addressList'}).data;
		addressList.map((item, index)=> {
			if (this.data.isChecked) {
				item.isDefault = false
			}
			if (id == item.id) {
				console.log(index)
				addressList[index] = {
				  province: this.data.cityValue[0],
				  city: this.data.cityValue[1],
				  area: this.data.cityValue[2],
				  provinceStr: this.data.cityText[0],
				  cityStr: this.data.cityText[1],
				  areaStr: this.data.cityText[2],
				  isDefault: this.data.isChecked,
				  realname,
				  street,
				  telephone,
				  // uid: this.$getUid(),
				  id: this.data.data.id,
				}
			}
		})
		my.setStorageSync({
		  key: 'addressList',
		  data: addressList,
		});
		setTimeout(()=> {
			my.navigateBack({
				delta: 1,
			})
		}, 50)
	  // this.$request(
	  //   "hzsx/userAddress/modifyUserAddress",
	  //   {
	  //     province: this.data.cityValue[0],
	  //     city: this.data.cityValue[1],
	  //     area: this.data.cityValue[2],
	  //     isDefault: this.data.isChecked,
	  //     realname,
	  //     street,
	  //     telephone,
	  //     // uid: this.$getUid(),
	  //     id: this.data.data.id,
	  //   },
	  //   "post"
	  // ).then((res) => {
	  //   my.navigateBack({
	  //     delta: 1,
	  //   })
	  // })
	},
	
	//提交按钮
	formSubmit(e) {
		console.log(e)
	  const { street, telephone, realname } = e.detail.value
	  if (!realname) {
	    my.showToast({ content: "请输入收货人姓名" })
	    return
	  }
	  if (!telephone || telephone.length !== 11) {
	    my.showToast({ content: "请输入正确的手机号" })
	    return
	  }
	  if (!this.data.cityText.length) {
	    my.showToast({ content: "请选择省市区" })
	    return
	  }
	  if (!street) {
	    my.showToast({ content: "请输入详细地址" })
	    return
	  }
	  if (this.data.isEdit) {
	    this.modify(street, telephone, realname)
	  } else {
	    this.add(street, telephone, realname)
	  }
	},
	//默认地址开关
	getSwitch(e) {
	  this.setData({
		  isChecked: !this.data.isChecked
	  })
	},
	//删除头部提示
	deleteTips() {
	  this.setData({
		  isTips: false
	  })
	},
	//删除
	deleteProvince() {
		const id = this.data.data.id
		let addressList = my.getStorageSync({key: 'addressList'}).data;
		addressList.map((item, index)=> {
			if (id == item.id) {
				console.log(index)
				addressList.splice(index, 1)
			}
		})
		my.setStorageSync({
		  key: 'addressList',
		  data: addressList,
		});
		setTimeout(()=> {
			my.navigateBack({
				delta: 1,
			})
		}, 50)
	  // this.$request(
	  //   "hzsx/userAddress/deleteUserAddress",
	  //   { id: this.data.data.id },
	  //   "get",
	  //   true
	  // ).then((res) => {
	  //   my.navigateBack({
	  //     delta: 1,
	  //   })
	  // })
	},
});